<?php
// 列表队列
set_time_limit(0);
//error_reporting(0);

include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Upyun.class.php');
include_once('../lib/Image.class.php');
include_once('../lib/Http.class.php');
include_once('../lib/Snoopy.class.php');
include_once('../lib/Cut.class.php');
include_once('../lib/function.php');

$redis = new Redis();
$redis->connect(REDIS_IP,"6379");  //php客户端设置的ip及端口
if(!$redis) die('redis connect fail');

$redis->auth(REDIS_PASS);

$url_key  = 'Gouyg_url_key';
$url_page = 'Gouyg_url_page';

$urls = array(
    //'qisuu,http://www.qisuu.com/s/new/index.html,http://www.qisuu.com/s/new/index_{page}.html',
    //'i7wu,http://www.i7wu.cn/newbook/,http://www.i7wu.cn/newbook/{page}.htm',
    'bookben,http://www.bookben.com/,http://www.bookben.com/?page={page}',
    //'ibook8,http://www.ibook8.com/dianzishu/html/newlist-1.html,http://www.ibook8.com/dianzishu/html/newlist-{page}.html',
);
$GLOBALS['config']['master']['db_database'] = 'suucn';
$GLOBALS['config']['slave']['db_database']  = 'suucn';

$pid = getmypid();
$redis->hSet(MH_PIDS, $pid, date('Y-m-d H:i:s').'||gouyg_queue_list');

$model = new Db;
$limit = 10;
while ($limit--) {
    $stop = $redis->get('LNPAN_QUEUE_STOP');
    if($stop) {
        break;
    }

    $item = (int)$redis->get($url_key);
    $page = (int)$redis->get($url_page);
    if($page == 0) $page = 1;
    if($item >= count($urls)) $item = 0;
    $items = $urls[$item];
    list($site, $url1, $url2) = explode(',', $items);
    $site = trim($site);

    $url = $page==1 ? $url1 : str_replace('{page}', $page, $url2);
    $parse = parse_url($url);
    $host  = $parse['host'];

    echo $url;
    $file  = getFile($url, $host);
    $html  = getList($file, $site);
    //print_r($html);exit;

    $newnum = $oldnum = $total = 0;
    $total = count($html['link']);
    foreach($html['link'] as $k=>$v){
        $old = $model->find('soso', "befrom='$site' and beurl='$v'");
        if(empty($old)) {
            $data = array(
                'befrom' => $site,
                'beurl' => trim($v),
            );
            $model->add('soso', $data);
            $newnum++;
        } else {
            $oldnum++;
        }
    }

    $log = array(
       'time' => date("Y-m-d H:i:s"),
       'num' => $total.'||'.$newnum.'||'.$oldnum, 
       'url' => $url.'||'.$page,
    );
    $redis->lPush('Gouyg_timer_list', json_encode($log));

    print_r(array('total'=>$total, 'new'=>$newnum, 'old'=>$oldnum));
    if($newnum > 0) {
        $page++;
    }
    else {
        $page = 1;
        $item++;
    }

    $redis->set($url_key,  $item);
    $redis->set($url_page, $page);

    //break;
}

$model->close();
$redis->hDel(MH_PIDS, $pid);

sleep(5);

function getList($web, $site){
    if($site=='bookben'){
        $split[0] = array('<UL class=all-list>','</UL>');
        $split[1] = '/<A href="(.+?)"/i';
    }
    elseif($site=='i7wu'){
        $split[0] = array('<div id="newbook">','<div class="pager">');
        $split[1] = '/<a href="(.+?)" class="fm">/i';
    }
    elseif($site=='ibook8') {
        $split[0] = array('<ul id="mainlistUL">','</ul>');
        $split[1] = '/<\/span><a href="(.+?)"/i'; 
    }
    $return = array();
    $web = iconv("GBK", "UTF-8//IGNORE", $web);
    $web = cut($split[0][0],$split[0][1],$web); 
    $web = preg_match_all($split[1],$web,$out);
    $return['link'] = $out[1];
    
    if($site == 'bookben') {
        global $redis;
        $return['link'] = array_slice($return['link'], 0, 10);
        $end = end($return['link']);
        preg_match('/\/txt\/(\d+)/i', $end, $match);
        $last_id = (int) $match[1];
        $cache = $redis->get('Gouyg_bookben_last_id');
        //var_dump($cache);
        if(!$cache) {
            $redis->set('Gouyg_bookben_last_id', $last_id);
        }
    }

    return $return;
}










